<?php
namespace Roomer\Controller;
use Think\Controller;
class EmployemanagerController extends Controller {
    public function index(){
        $this->checkSession();
        $this->recordList();
        $this->display();
    }


    public function recordList(){
        $this->checkSession();
        $record = M('employe');
        $total=$record->count();
        $per = 15;
        import('Org.Util.Page');
        $page = new \Page($total,$per);
        $sql = "select * from hotel_employe ".$page->limit;
        $query = $record -> query($sql);
        $pageList=$page->fpage();
        $this->assign('param',$query);
        $this->assign('pageList',$pageList);
        $this->assign('num',1);
    }
    public function editDisplay(){
        $this->checkSession();
        $employe_id = $_GET['employe_id'];
        $info = M('employe')->where("employe_id=$employe_id")->find();
        //$this->assign('admin',$admin);
        $this->assign('info',$info);

    }

    public function edit_employe(){//显示编辑界面
        $this->checkSession();
        $this->editDisplay();
        $this->display();
    }
    public function edit_employees(){
        $this->checkSession();
        $employe = M('employe');
        $data['employe_id']=$_POST['employe_id'];
      //  $id = $data['employe_id'];
      //  $oldName =$employe->where("employe_id='$id'")->getField('employe_idcard');
        $data['employe_name'] = $_POST['employe_name'];
        $data['employe_password'] = $_POST['employe_password'];
        $data['employe_birthday']= $_POST['employe_birthday'];
        $data['employe_mobile']=$_POST['employe_mobile'];
        $data['employe_idcard'] = $_POST['employe_idcard'];
        $data['employe_address'] = $_POST['employe_address'];

     //   $employe_idcard= $data['employe_idcard'];

     //   $record=$employe->where("employe_idcard='$employe_idcard'")->count();
       // if($record==0||$oldName==$data['employe_IDcard']) {
            if ($employe->save($data)) {
                $this->success('更新数据成功', U('employemanager/index'));
            } else {
                $this->error('发生未知错误或者未进行数据更新，请重试');
            }
     /*   }else{
            $this->error('该身份证已经被注册，请重试');

        }*/

    }

    public function add_employe(){
        $this->checkSession();
        $this->display();
    }
    public function add_employees()
    {
        $this->checkSession();
        $data['employe_name'] = $_POST['employe_name'];
        //$data['password'] = MD5($_POST['user_password']);
        $data['employe_password'] =md5($_POST['employe_password']);
        $data['employe_birthday'] = $_POST['employe_birthday'];
        $data['employe_mobile'] = $_POST['employe_mobile'];
        $data['employe_idcard'] = $_POST['employe_idcard'];
        $data['employe_address'] = $_POST['employe_address'];
        $employe_idcard = $data['employe_idcard'];
        $employe = M('employe');
        $record = $employe->where("employe_idcard='$employe_idcard'")->count();
        if ($record == 0) {
            if ($employe->add($data)) {
                $this->success('添加员工资料成功',U('employemanager/index') );
            } else {
                $this->error('发生未知错误，添加用户失败');
            }
        } else {
            $this->error('该员工身份证已被注册，请重试' );

        }
    }

    public function delete_employe(){//删除员工
        $this->checkSession();
        $employe_id = $_GET['employe_id'];
        $employe = M('employe');
        //执行删除操作
        $employe->where("employe_id =$employe_id")->delete();
        $this->success('删除成功',U('employemanager/index'));
    }
    public function deleteSelect(){
        $this->checkSession();
        $getId = $_POST['id'];
        if(!$getId)
            $this->error('未选择记录');
        $getids = implode(',', $getId); //选择一个以上，就用,把值连接起来(1,2,3)这样

        $id = is_array($getId) ? $getids : $getId; //如果是数组，就把用,连接起来的值覆给$id,否则就覆获取到的没有,号连接起来的值
        D("user")->execute('DELETE FROM hotel_employe where employe_id IN ('.$id .')');
        $this->success('删除成功');

    }
    public function log(){
        $this->checkSession();
        $record = M('log');
        $total=$record->count();
        $per = 15;
        import('Org.Util.Page');
        $page = new \Page($total,$per);
        $sql = "select * from hotel_log ".$page->limit;
        $query = $record -> query($sql);
        $pageList=$page->fpage();
        $this->assign('param',$query);
        $this->assign('pageList',$pageList);
        $this->assign('num',1);
        $this->display();
    }
    public function check_log()
    {
        $this->checkSession();
        $str = $_POST['keyword'];
        if (empty($str) || is_null($str)) {
            $this->recordList_order();
        } else {
            $order = M('log');
            $key = '%' . $str . '%';
            $map['logid|operator|room_id|order_id'] = array('like', $key);
            $total = $order->where($map)->count();
            $per = 15;
            import('Org.Util.Page');
            $page = new \Page($total, $per);
            $sql = "select * from hotel_log where  logid like '%" . $str . "%' or operator like '%" . $str . "%'or room_id like '%" . $str . "%'or order_id like '%" . $str . "%' " . $page->limit;
            $query = $order->query($sql);
            $pageList = $page->fpage();
            $this->assign('param', $query);
            $this->assign('pageList', $pageList);
            $this->assign('num', 1);
        }
        $this->display();
    }
    public function recordList_order(){
        $this->checkSession();
        $record = M('log');
        $total=$record->count();
        $per = 15;
        import('Org.Util.Page');
        $page = new \Page($total,$per);
        $sql = "select * from hotel_log ".$page->limit;
        $query = $record -> query($sql);
        $pageList=$page->fpage();
        $this->assign('param',$query);
        $this->assign('pageList',$pageList);
        $this->assign('num',1);
    }
    public function check_today(){
        $this->checkSession();
        $now=date("Y-m-d 00:00:00");
        $record = M('log');
        $total=$record->where("time>='$now'")->count();
        $per = 15;
        import('Org.Util.Page');
        $page = new \Page($total,$per);
        $sql = "select * from hotel_log where time>='$now'".$page->limit;
        $query = $record -> query($sql);
        $pageList=$page->fpage();
        $this->assign('param',$query);
        $this->assign('pageList',$pageList);
        $this->assign('num',1);
        $this->display();
    }

    public function checkSession(){
        $roomer_id = $_SESSION['roomer_id'];
        if(isset($roomer_id)){
            $this->assign('roomer_id',$roomer_id);
        }else{
            $this->error("需先登录,方可进行查询！",U('Login/index'));
        }
    }

}